package io.babystar.modules.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.Query;
import io.babystar.common.utils.R;
import io.babystar.common.utils.ShiroUtils;
import io.babystar.modules.business.dao.*;
import io.babystar.modules.business.entity.*;
import io.babystar.modules.business.service.CkCategoryService;
import io.babystar.modules.business.service.CkProductService;
import io.babystar.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service("ckProductService")
public class CkProductServiceImpl extends ServiceImpl<CkProductDao, CkProductEntity> implements CkProductService {

    @Autowired
    private CkCategoryDao ckCategoryDao;
    @Autowired
    private CkInDao ckInDao;
    @Autowired
    private CkOutDao ckOutDao;
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private TeacherDao teacherDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        String shopId = (String)params.get("shopId");
        String categoryId = (String)params.get("categoryId");
        String productName = (String)params.get("productName");
        Long businessId = (Long) params.get("businessId");


        int type = userEntity.getType();
        List<String> shops = new ArrayList<>();
        //店长、教练只查询当前店铺的
        if(type==2||type==3){
            Long teacherId = userEntity.getParentTeacherId();
            TeacherEntity teacher = teacherDao.selectById(teacherId);
            shops.add("-1");
            shops.add(teacher.getShopId()+"");
        }

        IPage<CkProductEntity> page = this.page(
                new Query<CkProductEntity>().getPage(params),
                new QueryWrapper<CkProductEntity>()
                .eq(businessId!=null,"business_id",businessId)
                .eq(categoryId != null && !"".equals(categoryId),"category_id",categoryId)
                .eq(StringUtils.isNotBlank(shopId), "shop_id", shopId)
                .in(type==2||type==3,"shop_id",shops)  //分店只能查看总部和自己店的商品库存
                .like(productName != null && !"".equals(productName),"product_name",productName)
        );
        for(CkProductEntity ckProductEntity: page.getRecords()){
            CkCategoryEntity ckCategoryEntity = ckCategoryDao.selectById(ckProductEntity.getCategoryId());
            if(ckCategoryEntity != null){
                ckProductEntity.setCategoryName(ckCategoryEntity.getCategory());
            }
            ShopEntity shopEntity = shopDao.selectById(ckProductEntity.getShopId());
            if(shopEntity!=null){
                ckProductEntity.setShopName(shopEntity.getShopName());
            }
            if(ckProductEntity.getShopId() !=null && ckProductEntity.getShopId().intValue() == -1){
                ckProductEntity.setShopName("总部");
            }
        }
        return new PageUtils(page);
    }

    @Override
    @Transactional
    public R delete(Integer id){
        Map<String,Object> ckInMap = new HashMap<>();
        ckInMap.put("product_code",id);
        List<CkInEntity> ckInList = ckInDao.selectByMap(ckInMap);
        if(ckInList!=null&&ckInList.size()>0){
            return R.error("该商品有入库记录，不允许删除！");
        }

        List<CkOutEntity> ckOutList = ckOutDao.selectByMap(ckInMap);
        if(ckOutList!=null&&ckOutList.size()>0){
            return R.error("该商品有出库记录，不允许删除！");
        }
        this.baseMapper.deleteById(id);
        return R.ok("商品删除成功");
    }

    @Override
    public List<CkProductEntity> getProductBy(Map<String, Object> params){
        return this.baseMapper.getProductBy(params);
    }

}